import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import '@/styles/index.less'
import axios from 'axios'
import qs from 'qs'

Vue.config.productionTip = false
axios.defaults.baseURL = 'http://192.168.39.102:9501'
Vue.prototype.$http = axios
Vue.prototype.$qs = qs

router.beforeEach((to, form, next) => {
  //跳转登陆，删除sessionStorage
  if (to.path === '/login'){
    sessionStorage.removeItem('user')
  }
  //从sessionStorage获取用户名
  let user = sessionStorage.getItem('user')
  let unauth_paths = [
    '/login',
    '/register',
  ]
  //如果用户名获取失败并且路由不是登陆，重定向登陆
  if (!user){
    if (unauth_paths.indexOf(to.path) > -1) {
      next()
    }else{
      next({
        path: '/login'
      })
    }
  }else{
    //有登陆，路由继续
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
